$(function() {
	$('#dg-employee').datagrid({
		title : '员工信息',
		url : basePath + 'employee/getAllEmployee.do',
		pagination : true,
		rownumbers : true,
		pageList : [ 5, 10, 15, 20 ],
		fitColumns : true,
		fitRows: true,
		singleSelect : true,
//		toolbar : '#tb',
		columns : [ [ {
			field : 'id',
			title : '编号',
			width : 100,
			hidden : true
		}, {
			field : 'name',
			title : '姓名',
			width : 100
		}, {
			field : 'usr',
			title : '登录名',
			width : 100
		}, {
			field : 'pwd',
			title : '密码',
			width : 100,
			hidden : true
		}, {
			field : 'sex',
			title : '性别',
			width : 100,
			formatter : function(value, row, index) {
				if (row.sex == 'f') {
					return "female";
				} else if (row.sex == 'm') {
					return "male";
				} else {
					return "other";
				}
			}
		}, {
			field : 'age',
			title : '年龄',
			width : 100
		}, {
			field : 'department',
			title : '所属部门',
			width : 150
		}, {
			field : 'position',
			title : '现任职位',
			width : 150
		}, {
			field : 'idCount',
			title : '身份证',
			width : 150
		}, {
			field : 'addr',
			title : '地址',
			width : 150
		},{
			field : 'email',
			title : '邮箱',
			width : 100
		}]]
	});
});

//add employee
function add(){
	$('#add').window({
	    modal:true,
	    title:'增加员工',
	    collapsible:false,
	    minimizable:false,
	    maximizable:false,
	    closed:true
	});
	$('#add').window('open');
}

//save employee
function saveAddEmployee(){
	var name = $('#ff1 input[name="name"]').val();
	var sex = $('#sex1').combobox('getValue');
	var age = $('#ff1 input[name="age"]').val();
	var idCount = $('#ff1 input[name="idCount"]').val();
	var addr = $('#ff1 input[name="addr"]').val();
	var usr = $('#ff1 input[name="usr"]').val();
	var pwd = $('#ff1 input[name="pwd"]').val();
	var email = $('#ff1 input[name="email"]').val();
	if($.trim(pwd)!=""&&$.trim(pwd)!=null){
		var passwd = $.md5($.trim(pwd));
	}else{
		return false;
	}
	 
	if(name!=""&&sex!=""&&age!=""&&idCount!=""&&addr!=""){
		$.ajax({
			type : 'POST',
			url : basePath + 'employee/addEmployee.do',
			data : {
				'name': name,
				'sex': sex,
				'age': age,
				'idCount': idCount,
				'addr': addr,
				'usr': usr,
				'pwd': passwd,
				'email': email
			},
			dataType : 'json',
			success: function(msg){
				if(msg.result==true){
					$.messager.show({
						title:'提示',
						msg:'增加员工成功!',
						showType:'show',
						style:{
							right:'',
							top:document.body.scrollTop+document.documentElement.scrollTop,
							bottom:''
						}
					});
					$('#ff1>input').val('');
					$('#add').window('close');
					$('#dg-employee').datagrid('reload');
				}else{
					$.messager.alert('提示','服务器出错,增加失败！','info');
				}
			}
		})
	}else{
		$.messager.alert('提示','请输入必填项！','info');
	}
}

function undoAddEmployee(){
	$('#ff1>input').val('');
	$('#add').window('close');
}

function edit(){
	var selectedRow = $('#dg-employee').datagrid('getSelected');
	if(selectedRow==null){
		$.messager.alert('提示','请选择一行！','info');
	}else{
		var id = selectedRow.id;
		$('#ff2 input[name="name"]').val(selectedRow.name);
		$('#sex2').combobox('setValue', selectedRow.sex);
		$('#age').numberbox('setValue',selectedRow.age);
		$('#idCount').numberbox('setValue',selectedRow.idCount);
		$('#ff2 input[name="addr"]').val(selectedRow.addr);
		$('#ff2 input[name="name"]').validatebox('validate');
		$('#ff2 input[name="addr"]').validatebox('validate');
		$('#ff2 input[name="usr"]').val(selectedRow.usr);
		$('#ff2 input[name="pwd"]').val("");
		$('#ff2 input[name="email"]').val(selectedRow.email);
		$('#edit').window({
		    modal:true,
		    title:'编辑员工',
		    collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closed:true
		});
		$('#edit').window('open');
		$('#saveEditEmployee').click(function(){
			saveEditEmployee(id);
		});
	}
}

//save employee
function saveEditEmployee(id){
	var name = $('#ff2 input[name="name"]').val();
	var sex = $('#sex2').combobox('getValue');
	var age = $('#ff2 input[name="age"]').val();
	var idCount = $('#ff2 input[name="idCount"]').val();
	var addr = $('#ff2 input[name="addr"]').val();
	var usr = $('#ff2 input[name="usr"]').val();
	var pwd = $('#ff2 input[name="pwd"]').val();
	var email = $('#ff2 input[name="email"]').val();
	if($.trim(pwd)!=""&&$.trim(pwd)!=null){
		var passwd = $.md5($.trim(pwd));
	}else{
		return false;
	}
	
	if(name!=""&&sex!=""&&age!=""&&idCount!=""&&addr!=""){
		$.ajax({
			type : 'POST',
			url : basePath + 'employee/updateEmployee.do',
			data : {
				'id': id,
				'name': name,
				'sex': sex,
				'age': age,
				'idCount': idCount,
				'addr': addr,
				'usr': usr,
				'pwd': passwd,
				'email': email
			},
			dataType : 'json',
			success: function(msg){
				if(msg.result==true){
					$.messager.show({
						title:'提示',
						msg:'修改员工成功!',
						showType:'show',
						style:{
							right:'',
							top:document.body.scrollTop+document.documentElement.scrollTop,
							bottom:''
						}
					});
					$('#ff2>input').val('');
					$('#edit').window('close');
					$('#dg-employee').datagrid('reload');
				}else{
					$.messager.alert('提示','服务器出错,修改失败！','info');
				}
			}
		})
	}else{
		$.messager.alert('提示','请输入必填项！','info');
	}
}

function undoEditEmployee(){
	$('#ff2>input').val('');
	$('#edit').window('close');
}

function deleteEmployee(){
	var selectedRow = $('#dg-employee').datagrid('getSelected');
	if(selectedRow==null){
		$.messager.alert('提示','请选择一行！','info');
	}else{
		var id = selectedRow.id;
		$.messager.confirm('确认对话框', '您想要删除该条记录吗？', function(r){
			if (r){
				$.ajax({
					type : 'POST',
					url : basePath + 'employee/deleteEmployee.do',
					data : {
						'id' : id
					},
					dataType : 'json',
					success: function(msg){
						if(msg.result==true){
							$.messager.show({
								title:'提示',
								msg:'删除员工成功!',
								showType:'show',
								style:{
									right:'',
									top:document.body.scrollTop+document.documentElement.scrollTop,
									bottom:''
								}
							});
							$('#dg-employee').datagrid('reload');
						}else{
							$.messager.alert('提示','服务器出错,删除失败！','info');
						}
					}
				})
			}
		});
	}
}

//assignJob
function assignJob(){
	var selectedRow = $('#dg-employee').datagrid('getSelected');
	if(selectedRow==null){
		$.messager.alert('提示','请选择一行！','info');
	}else{
		var id = selectedRow.id;
		$('#assignJob').window({
		    modal:true,
		    title:'职位分配',
		    collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closed:true
		});
		$('#assignJob').window('open');
		//二级联动
		var _department = $('#department').combobox({
            url: basePath + 'employee/getAllDepartment.do',
            editable: false,
            valueField: 'id',
            textField: 'text',
            onSelect: function (record) {
                _position.combobox({
                    disabled: false,
                    url: basePath + 'employee/getPositionByDep.do?department=' + record.text,
                    valueField: 'id',
                    textField: 'text'
                }).combobox('clear');
            }
        });
        var _position = $('#position').combobox({
            disabled: true,
            valueField: 'id',
            textField: 'text'
        }); 
        $('#saveJob').click(function(){
        	var department = $('#department').combobox('getText');
        	var position = $('#position').combobox('getText');
        	if(department==""||position==""){
        		$.messager.alert('提示','请选择部门职位！','info');
        	}else{       		
        		saveJob(id,department,position);
        	}
        });
	}
}

//保存职位
function saveJob(id,department,position){
	$.ajax({
		type : 'POST',
		url : basePath + 'employee/updateEmployeeJob.do',
		data : {
			'id' : id,
			'department' : department,
			'position' : position
		},
		dataType : 'json',
		success: function(msg){
			if(msg.result==true){
				$.messager.show({
					title:'提示',
					msg:'职位分配成功!',
					showType:'show',
					style:{
						right:'',
						top:document.body.scrollTop+document.documentElement.scrollTop,
						bottom:''
					}
				});
				$('#department').combobox('clear');
				$('#position').combobox('clear');
				$('#assignJob').window('close');
				$('#dg-employee').datagrid('reload');
			}else{
				$.messager.alert('提示','服务器出错,职位分配失败！','info');
			}
		}
	})
}

function undoSaveJob(){
	$('#department').combobox('clear');
	$('#position').combobox('clear');
	$('#assignJob').window('close');
}